package com.courseselector.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.courseselector.entity.user.User;
import com.courseselector.mapper.UserMapper;
import com.courseselector.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户服务实现类
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    /**
     * 学生角色ID
     */
    private static final Integer STUDENT_ROLE_ID = 3;
    
    /**
     * 教师角色ID
     */
    private static final Integer TEACHER_ROLE_ID = 2;

    @Override
    public Page<User> getStudentList(Page<User> page) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getRoleId, STUDENT_ROLE_ID);
        return this.page(page, queryWrapper);
    }

    @Override
    public Page<User> getTeacherList(Page<User> page) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getRoleId, TEACHER_ROLE_ID);
        return this.page(page, queryWrapper);
    }

    @Override
    public User getStudentById(Integer id) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getId, id).eq(User::getRoleId, STUDENT_ROLE_ID);
        return this.getOne(queryWrapper);
    }

    @Override
    public User getTeacherById(Integer id) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getId, id).eq(User::getRoleId, TEACHER_ROLE_ID);
        return this.getOne(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addStudent(User user) {
        user.setRoleId(STUDENT_ROLE_ID);
        // 默认状态为正常
        user.setStatus(1);
        return this.save(user);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addTeacher(User user) {
        user.setRoleId(TEACHER_ROLE_ID);
        // 默认状态为正常
        user.setStatus(1);
        return this.save(user);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateStudent(User user) {
        // 确保是学生角色
        User existUser = this.getById(user.getId());
        if (existUser == null || !STUDENT_ROLE_ID.equals(existUser.getRoleId())) {
            return false;
        }
        // 不允许修改角色
        user.setRoleId(STUDENT_ROLE_ID);
        return this.updateById(user);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateTeacher(User user) {
        // 确保是教师角色
        User existUser = this.getById(user.getId());
        if (existUser == null || !TEACHER_ROLE_ID.equals(existUser.getRoleId())) {
            return false;
        }
        // 不允许修改角色
        user.setRoleId(TEACHER_ROLE_ID);
        return this.updateById(user);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteStudent(Integer id) {
        // 确保是学生角色
        User user = this.getById(id);
        if (user == null || !STUDENT_ROLE_ID.equals(user.getRoleId())) {
            return false;
        }
        return this.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteTeacher(Integer id) {
        // 确保是教师角色
        User user = this.getById(id);
        if (user == null || !TEACHER_ROLE_ID.equals(user.getRoleId())) {
            return false;
        }
        return this.removeById(id);
    }
} 